/*
 * 12/08/2009 - dw - Updated the Query to retrieve Portals to account for the new localization
 *                      portal table.
 * 
 * */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Data;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Framework;
using DotNetNuke.Framework.Providers;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using System.Data;

namespace ITCrossing
{
	public partial class itcMetaPost_ManagePortals : PortalModuleBase
    {

		protected override void OnInit(EventArgs e)
        {
			string databaseOwner = MetaPostServices.DatabaseOwner;
			string objectQualifier = MetaPostServices.ObjectQualifier;

            SqlDataSource1.SelectCommand = "IF EXISTS (select o.name, c.name from dbo.sysobjects o JOIN dbo.syscolumns c ON o.id = c.id where c.name like '%PortalName' and o.name = '%Portals')"
                                            + "	 EXEC ('SELECT DISTINCT p.PortalID, p.PortalName, mpp.ActiveLicense,"
                                            + "       mpp.LicenseBeginDate, mpp.LicenseEndDate, mpp.CustomerPaid,"
                                            + "       mpp.CustomerPaymentDate, mpp.PublishRoles"
                                            + "       FROM " + databaseOwner + objectQualifier + "Portals AS p WITH (NOLOCK)"
                                            + "       LEFT OUTER JOIN " + databaseOwner + objectQualifier + "itcMetaPostPortals AS mpp WITH (NOLOCK) ON p.PortalID = mpp.PortalId')"
                                            + "	                               "
                                            + "ELSE"
                                            + ""
                                            + "	EXEC ('SELECT DISTINCT p.PortalID, pl.PortalName, mpp.ActiveLicense,"
                                            + "       mpp.LicenseBeginDate, mpp.LicenseEndDate, mpp.CustomerPaid,"
                                            + "       mpp.CustomerPaymentDate, mpp.PublishRoles"
                                            + "       FROM " + databaseOwner + objectQualifier + "Portals AS p WITH (NOLOCK)"
                                            + "		JOIN " + databaseOwner + objectQualifier + "PortalLocalization AS pl WITH (NOLOCK) ON pl.PortalID = p.PortalID"
                                            + "    	LEFT OUTER JOIN " + databaseOwner + objectQualifier + "itcMetaPostPortals AS mpp WITH (NOLOCK) ON p.PortalID = mpp.PortalId')";



        	SqlDataSource1.UpdateCommand = "	INSERT INTO " + databaseOwner + "[" + objectQualifier + "itcMetaPostPortals] (PortalId) "
        	                               +
        	                               "	SELECT P.PortalId FROM " + databaseOwner + "[" + objectQualifier + "Portals] p "
        	                               +
        	                               "		LEFT JOIN " + databaseOwner + "[" + objectQualifier + "itcMetaPostPortals] mpp ON p.PortalId = mpp.PortalId "
        	                               + "	WHERE mpp.PortalId IS NULL;"
        	                               + "	"
        	                               +
        	                               "	UPDATE " + databaseOwner + "[" + objectQualifier + "itcMetaPostPortals] "
        	                               + "	SET ActiveLicense = @ActiveLicense, "
        	                               + "		LicenseBeginDate = @LicenseBeginDate, "
										   + "		LicenseEndDate = @LicenseEndDate, "
        	                               + "		CustomerPaid = @CustomerPaid, "
        	                               + "		CustomerPaymentDate = @CustomerPaymentDate, "
        	                               + "		PublishRoles = @PublishRoles "
        	                               + "	WHERE PortalID = @PortalId";

			base.OnInit(e);
			Load += new EventHandler(Page_Load);

		}

        protected void Page_Load(object sender, EventArgs e)
		{
			if (AJAX.IsInstalled())
            {
                AJAX.RegisterScriptManager();
            }
            
			lnkreturn.NavigateUrl = Globals.NavigateURL(TabId, "Admin", "mid=" + ModuleId);

			ContainerControl.Visible = (UserInfo.IsSuperUser) ? true : false;

        }

		protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.Pager)
			{
				DropDownList pageSizeDropDown = new DropDownList();
				//adds variants of pager size
				pageSizeDropDown.Items.Add("10");
				pageSizeDropDown.Items.Add("25");
				pageSizeDropDown.Items.Add("50");
				pageSizeDropDown.Items.Add("100");
				pageSizeDropDown.Items.Add("250");
				
				ListItem listItem = pageSizeDropDown.Items.FindByText(GridView1.PageSize.ToString());
				if (listItem != null)
					pageSizeDropDown.SelectedIndex = pageSizeDropDown.Items.IndexOf(listItem);

				pageSizeDropDown.AutoPostBack = true;
				
				pageSizeDropDown.SelectedIndexChanged += (pageSizeDropDown_SelectedIndexChanged);
				
				Table pageTable = e.Row.Cells[0].Controls[0] as Table;
				TableCell cell = new TableCell();
				cell.Style["padding-left"] = "10px";
				cell.Controls.Add(new LiteralControl(Localization.GetString("PageSize.Text", this.LocalResourceFile)));
				cell.Controls.Add(pageSizeDropDown);
				if (pageTable != null)
				{
					pageTable.Rows[0].Cells.Add(cell);
				}
			}
		}

		void pageSizeDropDown_SelectedIndexChanged(object sender, EventArgs e)
		{
			//changes page size
			GridView1.PageSize = int.Parse(((DropDownList)sender).SelectedValue);
			//binds data source
			//GridView1.DataSource = ...;
			GridView1.DataBind();
		}
    }
}